package com.hqd.ch03.v50.boot.context.properties.bind.handler;

import com.hqd.ch03.v50.boot.context.properties.bind.AbstractBindHandler;
import com.hqd.ch03.v50.boot.context.properties.bind.BindContext;
import com.hqd.ch03.v50.boot.context.properties.bind.BindHandler;
import com.hqd.ch03.v50.boot.context.properties.bind.Bindable;
import com.hqd.ch03.v50.boot.context.properties.source.ConfigurationPropertyName;
import com.hqd.ch03.v50.core.convert.ConverterNotFoundException;

public class IgnoreTopLevelConverterNotFoundBindHandler extends AbstractBindHandler {

    /**
     * Create a new {@link IgnoreTopLevelConverterNotFoundBindHandler} instance.
     */
    public IgnoreTopLevelConverterNotFoundBindHandler() {
    }

    /**
     * Create a new {@link IgnoreTopLevelConverterNotFoundBindHandler} instance with a
     * specific parent.
     *
     * @param parent the parent handler
     */
    public IgnoreTopLevelConverterNotFoundBindHandler(BindHandler parent) {
        super(parent);
    }

    @Override
    public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error)
            throws Exception {
        if (context.getDepth() == 0 && error instanceof ConverterNotFoundException) {
            return null;
        }
        throw error;
    }

}

